package reflect;

import reflect.annotation.AutoRunClass;
import reflect.annotation.AutoRunMenthod;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

public class Test6 {
    public static void main(String[] args) throws Exception {
        File dir = new File(
                Test6.class.getResource(".").toURI()
        );

        String packName = Test6.class.getPackage().getName();
//        System.out.println(packName);

        File[] subs = dir.listFiles(f -> f.getName().endsWith(".class"));
        for (File sub : subs) {
            String fileName = sub.getName();
            String classMame = fileName.substring(0, fileName.indexOf("."));
            Class cls = Class.forName(packName + "." + classMame);
            if (cls.isAnnotationPresent(AutoRunClass.class)) ;
            Object obj = cls.newInstance();
            Method[] methods = cls.getDeclaredMethods();
            for (Method method : methods) {
                if (method.isAnnotationPresent(AutoRunMenthod.class)) {
                    AutoRunMenthod ram = method.getAnnotation(AutoRunMenthod.class);
                    int value = ram.value();
//                    System.out.println(value);
//                    method.invoke(obj);
                    for (int i = 0; i < value; i++) {
//                        System.out.println(obj);
                        method.invoke(obj);
                    }
                }
            }
        }
    }
}
